package com.example.bysj_sb.service;


import com.example.bysj_sb.dao.PermissionDao;
import com.example.bysj_sb.dao.RolePermissionDao;
import com.example.bysj_sb.dao.UserDao;
import com.example.bysj_sb.domain.User;
import com.example.bysj_sb.domain.authority.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
//@Transactional注解：可以用在方法上和类上，用在类上代表这个类的所有公共非静态方法都将启用事务功能。
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation. REQUIRED)
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private PermissionDao permissionDao;
    //添加
    public Boolean add(User user){
        return userDao.add(user);
    }
    //根据id查找
    public User find(int id){
        User user = userDao.find(id);
        return user;
    }
    //查找全部
    public List<User> findAll(){
        List<User> users = userDao.findAll();
        return users;
    }
    //修改
    public Boolean update(User user){
        return userDao.update(user);
    }
    //删除
    public Boolean delete(int id){
        return userDao.delete(id);
    }
    public User findByName(String name){
        User user = userDao.findByName(name);
        if(user == null){
            return null;
        }else{
            List<Integer> pIds = rolePermissionDao.getPermissionId(user.getRole().getId());
            if(pIds != null && pIds.size() > 0){
                user.getRole().setPermissions(permissionDao.getPermissionsByIds(pIds));
            }
            return user;
        }
    }
    public Actor login(User user){
        return userDao.login(user);
    }

}